package com.qc.wintrax.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.qc.wintrax.BaseActivity;
import com.qc.wintrax.R;
import com.qc.wintrax.utils.LogUtil;
import com.qc.wintrax.utils.TimerUtil;

import butterknife.Bind;
import butterknife.ButterKnife;

public class SettingActivity extends BaseActivity {

    @Bind(R.id.test_txt)
    TextView testTxt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        final Chronometer chronometer = (Chronometer) findViewById(R.id.chronometer1);
        Button btnStart = (Button) findViewById(R.id.btnStart);
        Button btnStop = (Button) findViewById(R.id.btnStop);
        Button btnRest = (Button) findViewById(R.id.btnReset);
        final Button btnPC = (Button) findViewById(R.id.btnPC);
        final EditText edtSetTime = (EditText) findViewById(R.id.setTimeEdit);

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("--开始记时---");
                rangeTime = 0;
                // 设置开始讲时时间
                chronometer.setBase(SystemClock.elapsedRealtime());
                // 开始记时
                chronometer.start();
            }
        });

         //暂停或继续计时
        btnPC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO 自动生成的方法存根
                if (!PAUSE) {
                   //暂停计时
                    btnPC.setText("继续");
                   //从开始计时计时器运行的时间差。。。。。关键
                    rangeTime = SystemClock.elapsedRealtime() - chronometer.getBase();
               //Toast.makeText(MainActivity.this, "时间："+rangeTime, Toast.LENGTH_SHORT).show();
                    chronometer.stop();
                } else {
                    //继续计时
                    btnPC.setText("暂停");
                    //设置计时器的基时间。。。。。关键
                    chronometer.setBase(SystemClock.elapsedRealtime() - rangeTime);
                    chronometer.start();
                }
                PAUSE = !PAUSE;
            }
        });

        btnStop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
             // 停止
                chronometer.stop();
            }
        });
          // 重置
        btnRest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chronometer.setBase(SystemClock.elapsedRealtime());
            }
        });
        chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
            @Override
            public void onChronometerTick(Chronometer chronometer) {
                cc++;
                LogUtil.e("得到的计时---" + cc);
                testTxt.setText(TimerUtil.FormatMiss((int) cc));

// 如果开始计时到现在超过了startime秒
//                        if (SystemClock.elapsedRealtime()
//                                - chronometer.getBase() > startTime * 1000) {
//                            chronometer.stop();
//
//// 给用户提示
//                            showDialog();
//                        }
            }
        });
    }


    protected void showDialog() {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setIcon(R.mipmap.icon_01);
        builder.setTitle("警告").setMessage("时间到")
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                    }
                });
        AlertDialog dialog = builder.create();
        dialog.show();
    }


    private int startTime = 0;
    private boolean PAUSE = false;
    private long rangeTime;//
    private long cc = -1;


}